package com.videomeetting.legend.videomeetting.activity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TableLayout;

import com.videomeetting.legend.videomeetting.R;
import com.videomeetting.legend.videomeetting.activity.fragment.ChannelsFragment;
import com.videomeetting.legend.videomeetting.activity.fragment.FilesFragment;
import com.videomeetting.legend.videomeetting.activity.fragment.OnliveFragment;
import com.videomeetting.legend.videomeetting.activity.fragment.SettingFragment;
import com.videomeetting.legend.videomeetting.activity.fragment.TranscribeFragment;
import com.videomeetting.legend.videomeetting.activity.fragment.VoiceFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.net.ConnectivityManager.TYPE_ETHERNET;

/**
 * Created by legend on 18-6-11.
 */

public class SettingActivity extends FragmentActivity implements View.OnClickListener{
    @BindView(R.id.activity_setting_tab)
    TableLayout settingTab;
    @BindView(R.id.activity_setting_setting_btn)
    Button settingBtn;
    @BindView(R.id.activity_setting_voice_btn)
    Button voiceBtn;
    @BindView(R.id.activity_setting_transcribe_btn)
    Button transcribeBtn;
    @BindView(R.id.activity_setting_channel_btn)
    Button channelBtn;
    @BindView(R.id.activity_setting_file_btn)
    Button fileBtn;
    @BindView(R.id.activity_setting_onlive_btn)
    Button onliveBtn;
    @BindView(R.id.activity_setting_frame_fl)
    FrameLayout frame;
    @BindView(R.id.activity_setting_home_btn)
    Button home;
    FragmentTransaction transaction;
    ChannelsFragment channelsFragment;
    FilesFragment filesFragment;
    OnliveFragment onliveFragment;
    SettingFragment settingFragment;
    TranscribeFragment transcribeFragment;
    VoiceFragment voiceFragment;
    FragmentManager fm;
    Fragment mCurrentFragment;
    List<Button> buttonList;


//    private WifiManager my_wifiManager;
//    private ConnectivityManager connectivityManager;
//    private DhcpInfo dhcpInfo;
//    private boolean ethernetConnected=false;
//    private EthernetInfo ethernetInfo;
//    public String ipAddress = null;
//    public String maskAddress = null;
//    public String gatewayAddress = null;
//    public String dns1Address = null;
//    public String dns2Address = null;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        buttonList=new ArrayList<>();
        settingBtn.setOnClickListener(this);
        voiceBtn.setOnClickListener(this);
        transcribeBtn.setOnClickListener(this);
        channelBtn.setOnClickListener(this);
        fileBtn.setOnClickListener(this);
        onliveBtn.setOnClickListener(this);

        buttonList.add(settingBtn);
        buttonList.add(voiceBtn);
        buttonList.add(transcribeBtn);
        buttonList.add(channelBtn);
        buttonList.add(fileBtn);
        buttonList.add(onliveBtn);

        home.setOnClickListener(this);

//        my_wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
//        dhcpInfo = my_wifiManager.getDhcpInfo();
//        connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo[] allNetworks = connectivityManager.getAllNetworkInfo();
//        for (int i=0;i<allNetworks.length;i++) {
//            if (allNetworks[i].getState() == NetworkInfo.State.CONNECTED) {
//                NetworkInfo netWorkInfo = allNetworks[i];
//                if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//                    ethernetConnected = true;
//                }
//            }
//        }
////        ethernetConnected = connectivityManager.getNetworkInfo(TYPE_ETHERNET).isConnected();
//        ethernetInfo = new EthernetInfo();
//        ethernetInfo.getInfo(connectivityManager);
//        getNetWorkInfo();
        settingFragment = new SettingFragment();
        voiceFragment = new VoiceFragment();
        transcribeFragment=new TranscribeFragment();
        filesFragment = new FilesFragment();
        channelsFragment=new ChannelsFragment();
        onliveFragment = new OnliveFragment();

        fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();
        transaction.add(R.id.activity_setting_frame_fl, settingFragment, "settingFragment")
                .add(R.id.activity_setting_frame_fl, channelsFragment, "channelsFragment").hide(channelsFragment)
                .add(R.id.activity_setting_frame_fl, filesFragment, "filesFragment").hide(filesFragment)
                .add(R.id.activity_setting_frame_fl, onliveFragment, "onliveFragment").hide(onliveFragment)
                .add(R.id.activity_setting_frame_fl, transcribeFragment, "transcribeFragment").hide(transcribeFragment)
                .add(R.id.activity_setting_frame_fl, voiceFragment, "voiceFragment").hide(voiceFragment)
                .commit();
//        transaction.add(R.id.activity_setting_frame_fl,settingFragment);
//        transaction.commit();
//        transaction.show(settingFragment);
    }
//    private void getNetWorkInfo(){
//        if (ethernetConnected){
//            ipAddress = ethernetInfo.ipAddress;
//            maskAddress = ethernetInfo.maskAddress;
//            gatewayAddress = ethernetInfo.gatewayAddress;
//            dns1Address = ethernetInfo.dns1Address;
//            dns2Address = ethernetInfo.dns2Address;
//        }else {
//            ipAddress = intToIp(dhcpInfo.ipAddress);
//            maskAddress = intToIp(dhcpInfo.netmask);
//            gatewayAddress = intToIp(dhcpInfo.gateway);
//            dns1Address = intToIp(dhcpInfo.dns1);
//            dns2Address = intToIp(dhcpInfo.dns2);
//        }
//        Log.i("legend", "getNetWorkInfo: ip"+ipAddress);
//        Log.i("legend", "getNetWorkInfo: gateway"+gatewayAddress);
//        Log.i("legend", "getNetWorkInfo: mask"+maskAddress);
//        Log.i("legend", "getNetWorkInfo: dns1"+dns1Address);
//        Log.i("legend", "getNetWorkInfo: dns2"+dns2Address);
//    }
//
//    private String intToIp(int paramInt) {
//        return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
//                + (0xFF & paramInt >> 24);
//    }
    // 使用 fragmentTransaction 隐藏fragment
    private void hideFragment(Fragment fragment, FragmentTransaction ft) {
        if (fragment != null) {
            ft.hide(fragment);
        }
    }
    public void switchFragment(FragmentTransaction fragmentTransaction,Button nowButton,Fragment nowFragment) {
        for(int i =0;i<buttonList.size();i++){
            buttonList.get(i).setBackgroundResource(R.drawable.button_unpressed);
        }
        nowButton.setBackgroundResource(R.drawable.button_pressed);
        hideFragment(settingFragment, fragmentTransaction);
        hideFragment(voiceFragment, fragmentTransaction);
        hideFragment(transcribeFragment, fragmentTransaction);
        hideFragment(filesFragment, fragmentTransaction);
        hideFragment(channelsFragment, fragmentTransaction);
        hideFragment(onliveFragment, fragmentTransaction);
//        if (settingFragment == null) {
//            //添加一个新的fragment
//            settingFragment = new SettingFragment();
//            fragmentTransaction.add(R.id.activity_setting_frame_fl, settingFragment);
//        } else {
            // 显示当前fragment
            mCurrentFragment= nowFragment;
        fragmentTransaction.show(nowFragment);
        fragmentTransaction.commit();
//        }
    }
    @Override
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch (v.getId()){
            //设置
            case R.id.activity_setting_setting_btn:
                switchFragment(fragmentTransaction,settingBtn,settingFragment);
                break;
            //声音
            case R.id.activity_setting_voice_btn:
                switchFragment(fragmentTransaction,voiceBtn,voiceFragment);
                break;
                //录制
            case R.id.activity_setting_transcribe_btn:
                switchFragment(fragmentTransaction,transcribeBtn,transcribeFragment);
                break;
                //通道
            case R.id.activity_setting_channel_btn:
                switchFragment(fragmentTransaction,channelBtn,channelsFragment);
                break;
                //文件
            case R.id.activity_setting_file_btn:
                switchFragment(fragmentTransaction,fileBtn,filesFragment);
                break;
                //直播
            case R.id.activity_setting_onlive_btn:
                switchFragment(fragmentTransaction,onliveBtn,onliveFragment);
                break;
                //主界面
            case R.id.activity_setting_home_btn:
                finish();
                break;

        }
    }
}
